//注册页面逻辑编写
$('#register').click(() => {
    let reg_username = $('#reg_username').val();
    let reg_password = $('#reg_password').val();
    let reg_password2 = $('#reg_password2').val();
    if (reg_username.length < 2 || reg_username.length > 16) {
        $('#modal_body').html('名字长度不能小于2个字符或者大于16个字符');
        $('#myModal').modal('show')
        return false;
    }
    if (reg_password.length < 6 || reg_password2.length < 6) {
        $('#modal_body').html('密码最少是6个字符');
        $('#myModal').modal('show')
        return false;
    }
    if (reg_password2 != reg_password) {
        $('#modal_body').html('两次密码输入需要一致');
        $('#myModal').modal('show')
        return false;
    }
    $.ajax({
        url: "/reg",
        type: 'post',
        dataType: 'json',
        data: {
            username: reg_username,
            password: reg_password
        },
        success: function (res) {
            if(res.code==0){
                //跳转到登录页
                location.href = '/login'
             }else{
                $('#modal_body').html(res.message);
                $('#myModal').modal('show')
            }
        },
        error: function (err) {
            console.log(err)
        }
    });
})

//用户登录


$('#login').click(() => {
    let username = $('#username').val();
    let password = $('#password').val();
    if (username == '' || password == '') {
        $('#modal_body').html('表单不能为空');
        $('#myModal').modal('show')
        return false;
    }
    $.ajax({
        url: "/log",
        type: 'post',
        dataType: 'json',
        data: {
            username: username,
            password: password
        },
        success: function (res) {
            if(res.code==0){
               //跳转到首页
               location.href = '/'
            }else{
                $('#modal_body').html(res.message);
                $('#myModal').modal('show')
            }
        },
        error: function (err) {
            console.log(err)
        }
    });
})